<template>
  <div class="container">
    <a-card class="general-card">
      <a-page-header :title="title" @back="goBack"> </a-page-header>
      <a-divider style="margin-top: 0" />
      <a-table
        row-key="id"
        :loading="loading"
        :data="renderData"
        :bordered="false"
        :pagination="false"
      >
        <template #columns>
          <a-table-column title="序号" :width="60">
            <template #cell="{ rowIndex }">
              {{ rowIndex + 1 }}
            </template>
          </a-table-column>
          <a-table-column title="享受旗区" data-index="areaName" />
          <a-table-column title="录入地区" data-index="sourceAreaName" />
          <a-table-column title="开户银行名称" data-index="bankName" />
          <a-table-column title="银行卡号" data-index="psnBankCode" />
          <a-table-column title="周期" data-index="cycleName" />
          <a-table-column title="数据状态" data-index="deleteFlag">
            <template #cell="{ record }">
              {{ record.deleteFlag === '1' ? '无效' : '有效' }}
            </template>
          </a-table-column>
          <a-table-column
            :title="$t('columns.operations')"
            align="right"
            data-index="operations"
            :width="250"
          >
            <template #cell="{ record }">
              <a-button
                type="text"
                size="small"
                @click="handleClickToSave(record)"
              >
                详情
              </a-button>
            </template>
          </a-table-column>
        </template>
      </a-table>
    </a-card>
  </div>
</template>

<script lang="ts" setup>
  import { ref } from 'vue';
  import useLoading from '@/hooks/loading';
  import {
    applyApplys,
    ApplyDetailVO,
    ApplyVO,
    PersonInfoVO,
  } from '@/api/subsidy';

  const { loading, setLoading } = useLoading();
  const renderData = ref<ApplyVO[]>([]); // ref<PersonVo[]>([]);
  const emit = defineEmits(['go']);
  const person = ref<PersonInfoVO>();
  const title = ref('居民享受记录');
  const fetchData = async (
    personId: string // = { current: 1, pageSize: 20 }
  ) => {
    setLoading(true);
    try {
      const { data } = await applyApplys(personId);
      renderData.value = data;
    } catch (err) {
      // you can report use errorHandler or other
    } finally {
      setLoading(false);
    }
  };
  const handleClickToSave = (obj?: ApplyDetailVO) => {
    emit('go', '3', undefined, obj);
  };
  const handleChangeVisible = (obj: PersonInfoVO) => {
    person.value = obj;
    title.value = `${person.value?.name} 享受记录`;
    fetchData(obj.id);
  };

  const goBack = () => {
    emit('go', '1');
  };
  defineExpose({ handleChangeVisible });
</script>

<style scoped lang="less">
  .container {
    padding: 10px;
  }

  :deep(.arco-table-th) {
    &:last-child {
      .arco-table-th-item-title {
        margin-left: 16px;
      }
    }
  }
</style>
